package com.hb.trade.server.project.config.task;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.hb.core.pojo.project.order.entity.UserOrder;
import com.hb.trade.server.project.order.service.UserOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * 固定时间的配置类
 *
 * @author zhaojk
 */
@Slf4j
@Configuration
@EnableAsync
public class OrderTask {

    protected final UserOrderService userOrderService;

    public OrderTask(UserOrderService userOrderService) {
        this.userOrderService = userOrderService;
    }

    /**
     * 定时任务测试
     * fixedRate: 单位毫秒
     */
    //@Async
    @Scheduled(fixedRate = 10000)
    public void deleteOrderData() {
        List<UserOrder> userOrders = userOrderService.deleteOrderData(15);
        if (CollUtil.isEmpty(userOrders)) {
            return;
        }
        for (UserOrder userOrder : userOrders) {
            log.info("订单[{}]超过{}分钟未支付, 创建时间{}, 已被删除", userOrder.getOutOrderNo(), 15, userOrder.getCreateTime());
        }
        log.info("已删除过期订单数: {}", userOrders.size());
    }

}
